package com.salou.cellar.activity;

import java.io.IOException;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.salou.cellar.R;
import com.salou.cellar.contentprovider.CellarContentProvider;

public class ConfigureActivity extends Activity implements OnClickListener {

	private static final int DBLOAD_PROGRESS_BAR = 1;
	private static final int DBLOAD_DONE = 2;
	private CellarContentProvider ccp;
	private Button reloadButton;
	private Button backButton;
	private ProgressDialog mProgressDialog;
	private int max;
	private Handler mProgressHandler;
	private int mProgress;
	private AlertDialog mDoneDialog;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ccp = new CellarContentProvider(getApplicationContext());
		setContentView(R.layout.configure_application);
		this.reloadButton = (Button) findViewById(R.id.reloadDB);
		this.reloadButton.setOnClickListener(this);
		this.backButton = (Button) findViewById(R.id.back_button);
		this.backButton.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		if (v.equals(reloadButton)) {
			try {
				this.max = ccp.getUploadableBottlesNumber();
			} catch (IOException e) {
				// problem with file
				e.printStackTrace();
			}
			if (this.max != 0) {
				showDialog(DBLOAD_PROGRESS_BAR);
				mProgress = 0;
				mProgressDialog.setProgress(0);
//				mProgressDialog.getButton(
//				DialogInterface.BUTTON_NEGATIVE)
//				.setEnabled(false);
				Runnable r = new Runnable() {
					@Override
					public void run() {
						System.out.println("db reload is running in thread "+Thread.currentThread().getName());
						ccp.deleteDB();
						ccp.createDB();
						ccp.firstLoad(mProgressHandler);
					}
				};
				Thread t=new Thread(r);
				t.start();
			} else {
			}
		} else if (v.equals(backButton)) {
			back();
		}
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case DBLOAD_PROGRESS_BAR:
			mProgressDialog = new ProgressDialog(this);
			mProgressHandler = new Handler() {
				@Override
				public void handleMessage(Message msg) {
					super.handleMessage(msg);
					if (msg.what==-1) {
						mProgressDialog.dismiss();
						showDialog(DBLOAD_DONE);
					} else {
						mProgress++;
						mProgressDialog.incrementProgressBy(msg.what);
					}
				}
			};
			mProgressDialog.setIcon(R.drawable.alert_dialog_icon);
			mProgressDialog.setTitle(R.string.reloadDBConfirmation);
			mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
			mProgressDialog.setMax(max);
			mProgressDialog.setButton(DialogInterface.BUTTON_POSITIVE,
					getText(R.string.background),
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog,
								int whichButton) {
							/* User clicked Yes so reload */
							System.out.println("onclick is running in thread "+Thread.currentThread().getName());
							mProgressDialog.dismiss();
						}
					});

			return mProgressDialog;

		case DBLOAD_DONE:
			String title = getResources().getString(R.string.reloadDBDone, max);
			mDoneDialog = new AlertDialog.Builder(this)
					.setIcon(R.drawable.alert_dialog_icon)
					.setTitle(title)
					.setPositiveButton(R.string.ok,
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int whichButton) {
									mDoneDialog.dismiss();
									back();
								}
							}).create();
			return mDoneDialog;
		}
		return null;
	}

	protected void back() {
		setResult(RESULT_OK);
		this.finish();
	}

}
